<template>
  <div>
    <search-table
      :url="url"
      ref="searchtable"
      :columns="columns"
      :defaultParams="defaultParams"
      :headParams="headParams"
      :form="form"
    >
      <template slot="searchForm">
        <a-form layout="inline" :form="form">
          <a-form-item label="状态">
            <a-select
              showSearch
              allowClear
              style="width: 150px"
              :options="statusList"
              v-decorator="['activityStatu']"
              placeholder="请选择"
            >
            </a-select>
          </a-form-item>
          <a-form-item label="活动名称">
            <a-input
              allowClear
              v-decorator="['activityName']"
              placeholder="请输入活动名称"
            ></a-input>
          </a-form-item>
          <a-form-item label="活动时间">
            <fast-range-picker v-decorator="['startDate-endDate']">
            </fast-range-picker>
            <!-- <a-date-picker
              style="width: 200px"
              allow-clear
              v-decorator="['startDate']"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
              placeholder="请选择开始时间" />
            <span style="margin: 0 10px;">至</span>
            <a-date-picker
              style="width: 200px"
              allow-clear
              v-decorator="['endDate']"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
              placeholder="请选择结束时间" /> -->
          </a-form-item>
          <!-- <a-form-item label="是否参与活动">
            <a-select
              allowClear
              style="width: 150px"
              :options="yesOrNo"
              v-decorator="['isIn']"
              placeholder="请选择">
            </a-select>
          </a-form-item> -->
        </a-form>
      </template>
      <template slot="oprationRow">
        <permission authority="seckill:activity:add">
          <a-button type="primary" @click="editInfo('add', {})"
            >新增活动</a-button
          >
        </permission>
      </template>
      <template slot="tableOperation" slot-scope="{ record }">
        <a-space>
          <permission
            v-if="record.activityStatu == 3"
            authority="seckill:activity:info"
          >
            <span class="text-primary pointer" @click="editInfo('view', record)"
              >查看</span
            >
            <!-- <a-button type="link">
              查看
            </a-button> -->
          </permission>
          <!-- <a-divider type="vertical" /> -->
          <permission
            v-if="record.activityStatu != 3"
            authority="seckill:activity:modify"
          >
            <span class="text-primary pointer" @click="editInfo('edit', record)"
              >编辑</span
            >
          </permission>
          <!-- <a-divider type="vertical" /> -->
          <permission
            v-if="record.activityStatu == 2"
            authority="seckill:activity:end"
          >
            <span
              class="text-primary pointer"
              @click="handleConfirm('end', record.id)"
              >立即结束</span
            >
          </permission>
          <!-- <a-divider type="vertical" /> -->
          <!-- <permission
            v-if="record.activityStatu != 3"
            authority="seckill:activity:end"
          >
            <span class="text-primary pointer" @click="chooseGoods(record)"
              >秒杀商品管理</span
            >
          </permission> -->
          <permission authority="seckill:activity:end">
            <span class="text-primary pointer" @click="chooseGoods(record)"
              >秒杀商品管理</span
            >
          </permission>
          <!-- <a-divider type="vertical" /> -->
          <permission authority="seckill:activity:delete">
            <span
              class="text-danger pointer"
              @click="handleConfirm('delete', [record.id])"
              >删除</span
            >
            <!-- <a-popconfirm
              title="是否确认删除？"
              :getPopupContainer="getPopupContainer()"
              @confirm="() => deleteRows(record.id)">
              <a href="javascript:;" class="text-danger pointer">删除</a>
            </a-popconfirm> -->
          </permission>
        </a-space>
      </template>
    </search-table>

    <seckillForm ref="seckillFormRef" @cancel="closeModal" />
  </div>
</template>

<script>
import { findDictNameByValue, createOptionsByDict } from "@/utils/dict";
import seckillForm from "./components/form";
export default {
  name: "MarketingSeckill",
  components: {
    seckillForm,
  },
  // props: {
  //   // 自身的tab
  //   tab: {
  //     type: String,
  //     required: true,
  //     default: ''
  //   },
  // },
  data() {
    return {
      statusList: createOptionsByDict("MARKETING_STATUS"),
      yesOrNo: createOptionsByDict("YES_NO"),
      url: "/seckill/activity/page",
      headParams: { baseURL: `${location.origin}/goldenhome/seckill/admin` },
      deleteUrl: "/seckill/activity/delete",
      handle: "edit",
      currentRecord: {},
      form: this.$form.createForm(this, { name: "table_search" }),
      defaultParams: {},
      columns: [
        {
          title: "活动名称",
          dataIndex: "activityName",
          key: "activityName",
        },
        {
          title: "活动时间",
          dataIndex: "activityTime",
          key: "activityTime",
        },
        {
          title: "活动状态",
          dataIndex: "activityStatu",
          key: "activityStatu",
          customRender: (text, record) =>
            findDictNameByValue("MARKETING_STATUS", record.activityStatu),
        },
        {
          title: "是否参与活动",
          dataIndex: "enableMspage",
          key: "enableMspage",
          customRender: (text, record) =>
            findDictNameByValue("YES_NO", record.enableMspage),
        },
        {
          title: "操作",
          dataIndex: "operation",
          scopedSlots: { customRender: "operation" },
        },
      ],
    };
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    closeModal(freshTable) {
      if (freshTable) {
        this.$refs.searchtable.refreshTable();
      }
    },
    handleConfirm(type, id) {
      let _this = this;
      switch (type) {
        case "delete":
          this.$confirm({
            title: "删除操作",
            content:
              "确定删除该活动，删除后将不可恢复，若已开启活动则该活动将被强制下架",
            okText: "确认删除",
            cancelText: "取消",
            onOk() {
              _this
                .$post("/seckill/activity/delete", id, {
                  baseURL: `${location.origin}/goldenhome/seckill/admin`,
                })
                .then((res) => {
                  if (res && res.returnCode == 1000) {
                    _this.$refs.searchtable.refreshTable();
                  }
                });
            },
            onCancel() {},
          });
          break;
        case "end":
          this.$confirm({
            title: "结束操作",
            content:
              "结束进行中的活动会影响买家正常购买，建议您提前告知或说明，确定要结束活动吗？",
            okText: "确认结束",
            cancelText: "取消",
            onOk() {
              _this
                .$get(
                  "/seckill/activity/end",
                  { id },
                  { baseURL: `${location.origin}/goldenhome/seckill/admin` }
                )
                .then((res) => {
                  if (res && res.returnCode == 1000) {
                    _this.$refs.searchtable.refreshTable();
                  }
                });
            },
            onCancel() {},
          });
          break;
      }
    },
    editInfo(type, record) {
      this.currentRecord = record;
      this.$refs.seckillFormRef.visible = true;
      this.$refs.seckillFormRef.handle = type;
      this.$refs.seckillFormRef.edit(record);
      // this.$router.push({
      //   name:'MarketingSeckillPagesForm',
      //   query: {
      //     handle: 'edit',
      //     id: record.id,
      //     status: record.activityStatu
      //   }
      // })
    },
    chooseGoods(record) {
      this.$router.push({
        name: "MarketingSeckillPagesCommodityManage",
        query: {
          activityId: record.id,
          enableMspage: record.enableMspage.toString(),
          activityStatu: record.activityStatu,
        },
      });
      // this.$router.push({
      //   name:'MarketingSeckillPagesChoose',
      //   query: {
      //     handle: 'edit',
      //     id: record.id
      //   }
      // })
    },
  },
};
</script>

<style lang="less" scoped></style>
